layui.use(['form','table','element','laytpl','layer','upload'], function() {
  var table = layui.table;
  var upload = layui.upload;
  table.render({
    elem: '#supplierTable'
    ,url:'/ssm/order/queryList.do'
    ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
    ,page : true
    ,height : "full-125"
    ,cols: [[
      {field:'orderNo', width:180, title: '订单编号'}
      ,{field:'recipients', title: '收货人'}
      ,{field:'tel', title: '电话'}
      ,{field:'address', title: '收货地址'}
      ,{field:'state', title: '订单状态',templet:function(d){
    	  var state = d.state==0?'订单已取消':(d.state==1?'待发货':(d.state==2?'待收货':'已收货'));
          return state;
      }}
      ,{field:'money', title: '总额'}
      ,{field:'ordertime', title: '创建时间'}
      ,{align:'center', toolbar: '#barTpl', width: 120, title: '操作'}
    ]]
  });
  

  //工具条点击事件
  	layui.table.on('tool(table)', function(obj){
  		var data = obj.data;
  		var layEvent = obj.event;

  		if(layEvent === 'edit'){ //修改
  			showEditModel(data);
  		} else if(layEvent === 'del'){ //删除
  			doDelete(data);
  		} else if(layEvent == 'detail'){
  			showPermDialog(data);
  		}else if(layEvent == 'operateAuth'){
  			perAuthWin(data.id,data.role_name);
  		}
  	});


  	//添加按钮点击事件
  	$("#addBtn").click(function(){
  		showEditModel(null);
  		return false;
  	});

  	$(".search_btn").on("click", function() {
        var queryValue=$("[name=queryValue]").val().trim();
        var queryKey=$("[name=queryKey]").val().trim();
        var postData={
        		tel:'',
        } //创建键值对象
        postData[queryKey]=queryValue;
        table.reload("supplierTable", {
            page : {
                curr : 1
            },
            where : postData
        })
    });

  	//显示表单弹窗
    function showEditModel(data){
        layer.open({
            type: 1,
            title: data==null?"添加":"修改",
            area: '90%',
            offset: 'auto',
            content: $("#addModel").html()
        });
        if(data!=null){
            $("#editForm input[id=cid]").val(data.cid);
            $("#editForm input[id=cname]").val(data.cname);
        }
        $("#btnCancel").click(function(){
            layer.closeAll('page');
        });

    }

    	//表单提交事件
    layui.form.on('submit(btnSubmit)', function(data){
            var qUrl="/ssm/order/save.do";
            $.post(qUrl,{
                cid : $("#cid").val(),
                cname : $("#cname").val(),
            },function(res){
             if(res.code=="01"){
                 layer.closeAll();
                 layer.msg("保存成功");
                 //刷新父页面
                 layui.table.reload('supplierTable', {});
             }else{
                 layer.msg("保存失败");
             }
         });
            return false;
    });

    
	//删除
	function doDelete(data){
        layer.confirm('是否确认发货？',{icon:3, title:'提示信息'},function(index){
            $.post("/ssm/order/updateStatus.do",{
            		oid : data.oid,
            		state:2,
            },function(data){
                if(data.code=="01"){
                	layer.close(index);
                	layui.table.reload('supplierTable', {});
                    
                }else{
                	layer.close(index);
                    layer.alert(data.body);
                    
                }

            })
        });
	}
	
	function showPermDialog(data){
   	 layer.open({
            type: 1,
            title: data=="详情",
            area: '90%',
            offset: 'auto',
            content: $("#detailModel").html()
        });
   	 
        if(data!=null){
       	 table.render({
    		    elem: '#supplierTable3'
    		    ,url:'/ssm/order/queryOrderProduct.do'
    		    ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
    		    ,page : false
    		    ,where:{
    		    	oid:data.oid
    		    }
    		    ,cols: [[
    		    	{field:'pname', title: '商品名称'}
    		       ,{field:'estoreprice', title: '单价'}
    		      ,{field:'buynum', title: '购买数量'}
    		    ]]
    		  });
        }
        $("#btnCancel").click(function(){
            layer.closeAll('page');
        });
   }

});